package cn.nkjobsearch.convert;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.nkjobsearch.mysql.MysqlConn;

public class ConvertData {
	public ConvertData() {
		mysql = new MysqlConn();
	}
MysqlConn m = new MysqlConn();
KeywordIndex K = new KeywordIndex(m);

	public void go(int max) {
		int cnt = 0;
		while(cnt < max) {
			if(getData() == 0) {
				System.out.println(cnt);
				break;
			}
			++cnt;
		}
		mysql.close();
	}

	/**
	 * @return 1&nbsp;&nbsp;表示此条数据转换成功<br/>
	 * 2&nbsp;&nbsp;表示此条数据转换失败<br/>
	 * 0&nbsp;表示无可转换数据
	 * @throws SQLException 
	 * */
	private int getData() {
		String sql = "SELECT id, title, orgName, city, exp, pubDate, edu, "
				+ "category, salary, jobIntro, jobNature, fromSite, jobURLId FROM `jobInfo` WHERE `convertFlag` = '0' LIMIT 0,1;";
		ResultSet rs = mysql.executeQuery(sql);
		String id = null, title = null, orgName = null, pubDate = null;
		String city = null, exp = null, edu = null, category = null, salary = null;
		String jobIntro = null, jobNature = null, jobURLId = null;
		char fromSite = 'o';
		try {
			if (rs.next()) {
				id = rs.getString("id");
				title = rs.getString("title");
				orgName = rs.getString("orgName");
				city = rs.getString("city");
				exp = rs.getString("exp");
				pubDate = rs.getString("pubDate");
				edu = rs.getString("edu");
				category = rs.getString("category");
				salary = rs.getString("salary");
				jobIntro = rs.getString("jobIntro");
				jobNature = rs.getString("jobNature");
				fromSite = rs.getString("fromSite").charAt(0);
				jobURLId = rs.getString("jobURLId");
			} else {
				return 0;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		String sqlInsert = "INSERT INTO  `jobinfohandled` (  "
				+ "`id` ,  `title` ,  `orgName` ,  `city` ,  `exp` ,  "
				+ "`pubDate` ,  `edu` ,  `category` ,  `salary` ,  " +
				// "`jobIntro` , " +
				"`jobNature` , `fromSite` ,  `jobURLId` )" + "VALUES (" + "'"
				+ id
				+ "',  '"
				+ title
				+ "',  "
				+ (orgName == null ? "NULL" : "'" + orgName + "'")
				+ ",  '"
				+ City.convert(city, fromSite)
				+ "', '"
				+ Experience.convert(exp, fromSite)
				+ "',  '"
				+ pubDate
				+ "',  '"
				+ EduLevel.convert(edu, fromSite)
				+ "',  "
				+ "'"
				+ Category.convert(category, fromSite)
				+ "',  '"
				+ Salary.convert(salary, fromSite)
				+ "',  '"
				+ JobNature.convert(jobNature, fromSite)
				+ "',  '"
				+ fromSite
				+ "',  '"
				+ jobURLId
				+ "');";
		if(mysql.executeInsert(sqlInsert) && K.putKeywordt2KeywordIndex(id,title,jobIntro)) {
			String sqlUpdate = "UPDATE `jobInfo` SET `convertFlag` = '1' WHERE `id` = '" + id + "'";
			mysql.executeUpdate(sqlUpdate);
			return 1;
		} else {
			String sqlUpdate = "UPDATE `jobInfo` SET `convertFlag` = '2' WHERE `id` = '" + id + "'";
			mysql.executeUpdate(sqlUpdate);
			return 2;
		}
	}

	private MysqlConn mysql = null;
}
